package com.asj.house.interceptor;

import com.asj.house.domain.HouseUser;
import com.asj.house.enums.SessionEnum;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.Objects;

public class ForceLoginInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {

        HttpSession session = request.getSession();

        HouseUser user = (HouseUser) session.getAttribute(SessionEnum.USER.getCode());

        if(Objects.isNull(user)){
            response.sendRedirect("house-login.html");
            return false;
        }

        return true;
    }
}
